package demo12;

import java.util.ArrayList;
import java.util.List;

/**
 * 全排列
 * https://leetcode.cn/problems/VvJkup/
 */
class Solution {
    List<List<Integer>> ret = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    boolean[] check;
    int len;
    public List<List<Integer>> permute(int[] nums) {
        len = nums.length;
        check = new boolean[len];
        dfs(nums);
        return ret;
    }

    private void dfs(int[] nums) {
        if(path.size() == len) {
            ret.add(new ArrayList<>(path));
            return;
        }
        for(int i = 0; i < len; i++) {
           if(check[i] == false) {
               path.add(nums[i]);
               check[i] = true;
               dfs(nums);
               // 回溯 -> 恢复现场
               path.remove(path.size() -1);
               check[i] = false;
           }
        }
    }
}
class Demo {
    public static void main(String[] args) {
        Solution solution = new Solution();
        List<List<Integer>> permute = solution.permute(new int[]{1, 2, 3});
        for(List<Integer> nums : permute) {
            for(int num : nums) {
                System.out.print(num + " ");
            }
            System.out.println();
        }
    }
}